package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Exercise;
import com.itheima.pojo.Food;
import com.itheima.pojo.Intervention;
import com.itheima.service.HealthInterventionService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/intervention")
public class healthInterventionController {
    @Reference
    private HealthInterventionService healthInterventionService;

    @RequestMapping("/querySport")
    public Result querySport(){
        try {
            List<Exercise> list = healthInterventionService.querySport();
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    @RequestMapping("/queryFood")
    public Result queryFood(){
        try {
            List<Food> list = healthInterventionService.queryFood();
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }
    @RequestMapping("/verifyOrderStatus")
    public Result verifyOrderStatus(Integer id){
       Boolean flag = healthInterventionService.verifyOrderStatus(id);
       return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,flag);
    }
    @RequestMapping("/verifyIntervention")
    public Result verifyIntervention(Integer id){
       Boolean flag = healthInterventionService.verifyIntervention(id);
       return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,flag);
    }
    @RequestMapping("/addIntervention")
    public Result addIntervention(@RequestBody Intervention intervention){
        Integer order_id = intervention.getOrder_id();
        healthInterventionService.deleteIntervention(order_id);
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = user.getUsername();
        intervention.setDoctorName(name);
        try {
            Integer id = healthInterventionService.addIntervention(intervention);
            healthInterventionService.deleteInterventionAndExercise(id-1);
            healthInterventionService.deleteInterventionAndFood(id-1);
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS,id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }
    @RequestMapping("/addInterventionAndExercise")
    public Result addInterventionAndExercise(Integer interventionId,Integer[] sportIds){
        try {
            healthInterventionService.addInterventionAndExercise(interventionId,sportIds);
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }
    @RequestMapping("/addInterventionAndFood")
    public Result addInterventionAndFood(Integer interventionId,Integer[] foodIds){
        try {
            healthInterventionService.addInterventionAndFood(interventionId,foodIds);
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }
    @RequestMapping("/queryAdvice")
    public Result queryAdvice(Integer id){
        try {
            List<String> list = healthInterventionService.queryAdvice(id);
            return new Result(true,MessageConstant.ADD_SETMEAL_FAIL,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }
    @RequestMapping("/queryExerciseByIntervention")
    public Result queryExerciseByIntervention(Integer id){
        Integer interventionId = queryInterventionId(id);
        try {
            List<Integer> list = healthInterventionService.queryExerciseByIntervention(interventionId);
            return new Result(true,MessageConstant.ADD_SETMEAL_FAIL,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }
    @RequestMapping("/queryFoodByIntervention")
    public Result queryFoodByIntervention(Integer id){
        Integer interventionId = queryInterventionId(id);
        try {
            List<Integer> list = healthInterventionService.queryFoodByIntervention(interventionId);
            return new Result(true,MessageConstant.ADD_SETMEAL_FAIL,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }
    public Integer queryInterventionId(Integer id){
       Integer interventionId = healthInterventionService.queryInterventionId(id);
       return interventionId;
    }

}
